package com.google.mr.okhttptest;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.menu.MenuAdapter;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.bilibili.magicasakura.widgets.TintToolbar;
import com.google.mr.okhttptest.fragment.BaseFragment;
import com.google.mr.okhttptest.fragment.MainFragment;
import com.google.mr.okhttptest.fragment.TabNetPagerFragment;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.bar_net)
    ImageView barNet;
    @BindView(R.id.bar_music)
    ImageView barMusic;
    @BindView(R.id.bar_friends)
    ImageView barFriends;
    @BindView(R.id.bar_search)
    ImageView barSearch;
    @BindView(R.id.toolbar)
    TintToolbar toolbar;
    @BindView(R.id.main_viewpager)
    CustomViewPager customViewPager;
    @BindView(R.id.bottom_container)
    FrameLayout bottomContainer;
    @BindView(R.id.a)
    RelativeLayout a;
    @BindView(R.id.id_lv_left_menu)
    ListView idLvLeftMenu;
    @BindView(R.id.fd)
    DrawerLayout fd;
    private ArrayList<ImageView> mTabs = new ArrayList<>();
    private ActionBar actionBar;
    private ArrayList<ImageView> tabs = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        setToolbar();

        setViewPager();

        setUpDrawer();
    }

    /**
     * 设置侧拉菜单
     */
    private void setUpDrawer() {
        LayoutInflater layoutInflater = LayoutInflater.from(this);
        idLvLeftMenu.addHeaderView(layoutInflater.inflate(R.layout.nav_header_main,idLvLeftMenu,false));
        idLvLeftMenu.setAdapter(new MenuItemAdapter(this));
//        需要写布局界面,稍后再更
    }


    /**
     * 设置toolbar
     */
    private void setToolbar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.ic_menu);

    }

    /**
     * 设置ViewPager
     */
    private void setViewPager() {
        mTabs.add(barNet);
        mTabs.add(barMusic);
        mTabs.add(barFriends);
        //创建两个Fragment
        MainFragment mainFragment = new MainFragment();
        TabNetPagerFragment tabNetPagerFragment = new TabNetPagerFragment();
        //创建Fragment适配器
        final CustomViewPagerAdapter customViewPagerAdapter = new CustomViewPagerAdapter(getSupportFragmentManager());
        //将Fragment添加到适配器中
        customViewPagerAdapter.addFragment(tabNetPagerFragment);
        customViewPagerAdapter.addFragment(mainFragment);
        //设置他们之间的联动事件
        customViewPager.setAdapter(customViewPagerAdapter);
        customViewPager.setCurrentItem(1);
        barMusic.setSelected(true);
        customViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switchTabs(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        barNet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                customViewPager.setCurrentItem(0);
            }
        });

        barMusic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                customViewPager.setCurrentItem(1);
            }
        });
    }

    private void switchTabs(int position) {
        for (int i = 0; i < mTabs.size(); i++) {
            if (position==i){
                mTabs.get(i).setSelected(true);
            }else{
                mTabs.get(i).setSelected(false);
            }
        }
    }

    private class CustomViewPagerAdapter extends FragmentPagerAdapter {
        private final ArrayList<Fragment> mFragments = new ArrayList<Fragment>();

        public CustomViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        public void addFragment(Fragment fragment) {
            mFragments.add(fragment);
        }
    }
}
